package com.yl.module.common.controller;

import com.yl.module.base.controller.BaseController;
import com.yl.module.base.model.R;
import com.yl.module.common.service.IMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

/**
 * @Todo
 * @Author jiangtao
 * @CreateBy 2020-02-18 9:13 PM
 * @UpdateBy jiangtao
 * @Version v1.0
 */
@RestController
@RequestMapping("/mail")
public class MailController extends BaseController {

    private static final String TO = "1066985081@qq.com";

    @Autowired
    private IMailService mailService;
    @Autowired
    private TemplateEngine templateEngine;

    @RequestMapping("/send1")
    public R index() {
        try {
            mailService.sendSimpleMail(TO, "这是一封普通的邮件", "这是一封普通的SpringBoot问候邮件");
        } catch (Exception ex) {
            ex.printStackTrace();
            return error("普通邮件发送失败");
        }
        return success("普通邮件发送成功");
    }

    @RequestMapping("/send2")
    public R index2() {
        try {
            mailService.sendHtmlMail(TO, "这是一HTML的邮件", "<body>\n" +
                    "<div id=\"welcome\">\n" +
                    "    <h3>Welcome To My Friend!</h3>\n" +
                    "\n" +
                    "    GitHub：\n" +
                    "        <a href=\"#\" th:href=\"@{${github_url}}\" target=\"_bank\">\n" +
                    "            <strong>GitHub</strong>\n" +
                    "        </a>\n" +
                    "    <br />\n" +
                    "    <br />\n" +
                    "    个人博客：\n" +
                    "        <a href=\"#\" th:href=\"@{${blog_url}}\" target=\"_bank\">\n" +
                    "            <strong>DoubleFJ の Blog</strong>\n" +
                    "        </a>\n" +
                    "    <br />\n" +
                    "    <br />\n" +
                    "    <img width=\"258px\" height=\"258px\"\n" +
                    "         src=\"https://raw.githubusercontent.com/Folgerjun/materials/master/blog/img/WC-GZH.jpg\">\n" +
                    "    <br />微信公众号（诗词鉴赏）\n" +
                    "</div>\n" +
                    "</body>");

        } catch (Exception ex) {
            ex.printStackTrace();
            return error("HTML邮件发送失败");
        }
        return success("HTML邮件发送成功");
    }

    @RequestMapping("/send3")
    public R index3() {
        try {
            String imgPath = "E:\\j123.jpg";
            mailService.sendAttachmentsMail(TO, "这是一封带附件的邮件", "这是一封附件的SpringBoot问候邮件", imgPath);
        } catch (Exception ex) {
            ex.printStackTrace();
            return error("附件邮件发送失败");
        }
        return success("附件邮件发送成功");
    }

    @RequestMapping("/send4")
    public R index4() {
        try {
            String imgPath = "E:\\j123.jpg";
            String rscId = "DoubleFJ";
            String content = "<html><body>这是有图片的邮件<br/><img src=\'cid:" + rscId + "\' ></body></html>";
            mailService.sendResourceMail(TO, "这邮件中含有图片", content, imgPath, rscId);

        } catch (Exception ex) {
            ex.printStackTrace();
            return error("静态资源邮件发送失败");
        }
        return success("静态资源邮件发送成功");
    }

    @RequestMapping("/send5")
    public R templateMail(){
        try {
            Context context = new Context();
            context.setVariable("github_url", "https://github.com/Folgerjun");
            context.setVariable("blog_url", "http://putop.top/");
            String emailContent = templateEngine.process("mail/mailTemplate", context);

            mailService.sendHtmlMail(TO, "这是模板邮件", emailContent);
        }catch (Exception ex){
            ex.printStackTrace();
            return error("模板邮件发送失败");
        }
        return success("模板邮件发送成功");
    }

}
